<!DOCTYPE HTML>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Demo: Basic Graphics</title>
		<link rel="stylesheet" href="style.css" media="screen">
		<link rel="stylesheet" href="http://dojotoolkit.org/documentation/tutorials/1.7/resources/style/demo.css" media="screen">
		<script type="text/javascript">
			function zincPluginReady() {
            	// the zinc plugin is ready, so we can start using it.
                console.log("The zinc plugin is now ready to go!");
				// grab a handle to the plugin object
        		var plugin = document.getElementById("zincPlugin");
        		/* create a download manager for reading input files */
        		var downloadManager = plugin.createDownloadManager();
        		/* add in URIs for downloadManager to read files from.
        			In this case files from the local directories. Each item stores
        			a block of memory for each file. */
        		var nodesItem = downloadManager.addURI("../resources/cube.exnode");
        		var elementsItem = downloadManager.addURI("../resources/cube.exelem");
        		/* add in a completion callback function once the download manager has 
        			 finish downloading and put the files into memory. In this case,
        			 the function returned by the getCubeDownloadCompleteFunction() will
        			 be called once download is finished. We wrap the callback function this
        			 way to provide scope to the arguments of the callback function.
        			 */
        		downloadManager.addCompletionCallback(
        			getCubeDownloadCompleteFunction(plugin, nodesItem, elementsItem));
            }
			/* Use a wrapper to preserve the arguments until the callback is made,
			most of the function calls here are similar to this. */
			function getCubeDownloadCompleteFunction(pluginIn, nodesItemIn, elementsItemIn) {
				console.log("getting the cube download complete callback function.");
				var zincPlugin = pluginIn;
				var nodesItem = nodesItemIn;
				var elementsItem = elementsItemIn;

				// this is the actual function that will be called upon completion of the downloads
				return function() {
					// get a handle to the default root region
					var rootRegion = zincPlugin.context.getDefaultRegion();
					// create a stream information object which will be used to define the data to read 
					var streamInformation = rootRegion.createStreamInformation();
					// and add the node and element items to the stream information
					streamInformation.createResourceDownloadItem(nodesItem);
					streamInformation.createResourceDownloadItem(elementsItem);
					// Now we can load the mesh from the memory stream.
					rootRegion.read(streamInformation);
					
					// if successful we should now be able to get the cube region just loaded.
					var cubeRegion = rootRegion.findSubregionAtPath("cube");
					if (cubeRegion) {
						// FIXME: is there a better way to check for success?
						console.log("found the cube region");
						// do some basic set-up of the scene viewer. 
						var sceneViewer = zincPlugin.sceneViewer;
						sceneViewer.setBackgroundColourRGB(1.0, 1.0, 1.0);
						sceneViewer.antialiasMode = 4;
						// Now we get a handle to the default graphics module for this zinc context 
						var graphicsModule = zincPlugin.context.getDefaultGraphicsModule();
						// and the rendition object for the cube's region 
						var cubeRendition = graphicsModule.getRendition(cubeRegion);
						// and define standard materials for the graphics module 
						graphicsModule.defineStandardMaterials();
						// Now we are able to layout the scene we want to view in the scene viewer
						cubeRendition.beginChange(); // FIXME: cache changes to improve performance?
						// API for the rendition object is still lacking, so we make use of the type-specific
						// execute command method.
						cubeRendition.executeCommand("general clear default_coordinate coordinates");
						cubeRendition.executeCommand("cylinders constant_radius 0.05 material green");
						cubeRendition.executeCommand("node glyph sphere size '0.15*0.15*0.15' material blue");
						// finished making changes to the rendition, so end the caching and execute the changes.
						cubeRendition.endChange();
						// reset the scene to ensure all visible graphics are shown.
						sceneViewer.viewAll();
					} else {
						alert("Something went horribly wrong loading the cube region?");
					}
				}
			}
		</script>
	</head>
	<body>
		<h1>Demo: Basic Graphics</h1>
		<object id="zincPlugin"  type="application/x-zinc-plugin" style="width: 400px; height: 400px; border:5px solid red;" onzincready="zincPluginReady()"></object>
	</body>
</html>
